/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.dao.impl;

import java.util.List;
import mx.netbrain.dao.TypeFileDao;
import mx.netbrain.model.Type;
import mx.netbrain.util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author Julian
 */
public class TypeFileDaoImpl implements TypeFileDao {

    @Override
    public void save(Type typeFile) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.persist(typeFile);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al actualizar perfil");
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void update(Type typeFile) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(typeFile);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al actualizar perfil");
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Type findById(Long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Type) session.load(Type.class, id);
    }

    @Override
    public List<Type> listAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("from Type").list();
    }

    @Override
    public void delete(Type typeFile) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(typeFile);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al actualizar perfil");
            session.beginTransaction().rollback();
        }
    }
}
